package com.css.fxfzcpxx.async.service.impl;


import com.css.fxfzcpxx.async.service.AsyncService;
import com.css.fxfzcpxx.base.log.Log;
import com.css.fxfzcpxx.base.user.service.UserService;
import com.css.fxfzcpxx.base.user.viewobjects.UserBaseInfoVO;
import com.css.fxfzcpxx.modules.logRecord.entity.SysLog;
import com.css.fxfzcpxx.modules.logRecord.repository.LogRecordRepository;
import com.css.fxfzcpxx.util.PlatformDateUtils;
import com.css.fxfzcpxx.util.PlatformObjectUtils;
import com.css.fxfzcpxx.util.RedisLockUtil;
import com.css.fxfzcpxx.util.UUIDGenerator;
import com.css.fxfzcpxx.web.PlatformSessionContext;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.concurrent.Future;

/**
 * @Author:
 * @CreateTime: 2023/11/17 10:39
 * @Description:
 */
@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {

    @Autowired
    private UserService userService ;
    @Autowired
    private LogRecordRepository logRecordRepository;
    @Resource
    private RedisLockUtil redisLockUtil;

    @Autowired
    private TaskExecutor taskExecutor;

    @Async
    @Override
    public void saveLog(ProceedingJoinPoint point) {
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                saveLogfun(point);
            }
        });
    }
    /**
     * 记录浏览量
     *
     * @param
     */
    private void saveLogfun(ProceedingJoinPoint point) {
        String requestId = UUIDGenerator.getUUID();
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();

        SysLog sysLog = new SysLog();
        Log LogRecord = method.getAnnotation(Log.class);
        Boolean isLocked = redisLockUtil.lock("redis_eventCode_lock_serialNum", requestId, 6L);
        if (isLocked){
            log.info("成功获取redis_eventCode_lock_serialNum锁");
            if (LogRecord != null) {
                String releaseColumn = "";
                //类型
                String type = LogRecord.type();
                //模块
                String module = LogRecord.value();
                // 获取方法上的GetMapping注解
                GetMapping requestMapping = method.getAnnotation(GetMapping.class);
                // 获取接口路径
                String interfacePath = requestMapping.value()[0];
                /*if("/claCasPage".equals(interfacePath)){
                    ClaCasParams args = (ClaCasParams) point.getArgs()[0];
                    //栏目
                    releaseColumn=args.getReleaseColumn();
                    if("典型案例".equals(releaseColumn)){
                        sysLog.setModules(releaseColumn);
                        module=releaseColumn;
                    }
                }*/
                SysLog data=logRecordRepository.findByModuleAndType(module,type);
                if(PlatformObjectUtils.isNotEmpty(data)){
                    logRecordRepository.updatePageView(data.getLogId(),data.getPageView()+1);
                }else {
                    sysLog.setLogId(UUIDGenerator.getUUID());
                    sysLog.setOpType(type);
                    sysLog.setModules(module);
                    sysLog.setOpTime(PlatformDateUtils.getCurrentTimestamp());
                    sysLog.setPageView(1);
                    sysLog.setSign("0");
                    if(PlatformObjectUtils.isNotEmpty(PlatformSessionContext.getUserID())){
                        UserBaseInfoVO user = userService.getUser(PlatformSessionContext.getUserID());
                        if(PlatformObjectUtils.isNotEmpty(user)){
                            sysLog.setOpUser(user.getUserName());
                        }
                    }
                    logRecordRepository.save(sysLog);
                }
                redisLockUtil.releaseLock("redis_distributed_lock_serialNum", requestId);
                log.info("成功释放redis_distributed_lock_serialNum锁");
            }
        }else {
            log.info("获取redis_eventCode_lock_serialNum锁失败，进行休眠");
            try {
                Thread.sleep(10000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }





    }
}
